package test;


import java.io.IOException;
import java.io.Serializable;
import java.rmi.AlreadyBoundException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;


import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import message.Message;
import message.MessageType;
import node.ActualNode;
import node.Node;

public class RequestClient {

	 public static void main(final String[] the_args) throws AlreadyBoundException, IOException {
		 final String host = the_args[0];
		    final int port = Integer.parseInt(the_args[1]);
		    final String registry_name = the_args[2];

		    try {
		      final Registry bootstrap = LocateRegistry.getRegistry(host, port);
		      final Registry registry = (Registry) bootstrap.lookup(registry_name);
		      
		      
		     Node my_node = new ActualNode(2, 3, registry);
		      registry.bind("node_id_" + 2, UnicastRemoteObject.exportObject(my_node, 0));
		      Node n = (Node) registry.lookup("node_id_2");
		      

		      System.out.println("This is node id " + n.getMyID());

	     

	//	      Message request1 = new Message(n.getMyID(), "ewfwea", "acsdb",MessageType.PUT_VALUE);
		//      System.out.println("sending message.............");

//		      Message request = new Message(n.getMyID(), "Tina", "Yuan" ,MessageType.PUT_VALUE);
//		      System.out.println("sending message.............  +" + request.getValue() );


		      Message request = (Message) n.put("Tina","Yuan");



//		      Thread.sleep(1000);
//		      request = new Message(n.getMyID(), "Tina",null, MessageType.GET_VALUE);
		      Message response = (Message)n.get("Tina");
		      if(response != null) {
	          System.out.println(response.getValue());		        
		      } else {
		        System.out.println("Doesn't find the value of this key!");
		      }
		    } catch (RemoteException e) {
		        // TODO Auto-generated catch block
		        e.printStackTrace();
		      } catch (NotBoundException e) { 
		        // TODO Auto-generated catch block
		        e.printStackTrace();
		      } /*catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			*/
	 }
}
